﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace TCPClientServer_Test
{
    class Program
    {
        //static bool closeSocket;

        //static private string GetComputer_InternetIP()
        //{
        //    // check IP using DynDNS's service
        //    WebRequest request = WebRequest.Create("http://checkip.dyndns.org");
        //    request.Proxy = null;
        //    WebResponse response = request.GetResponse();
        //    StreamReader stream = new StreamReader(response.GetResponseStream());

        //    // IMPORTANT: set Proxy to null, to drastically INCREASE the speed of request
        //    //request.Proxy = null;

        //    // read complete response
        //    string ipAddress = stream.ReadToEnd();

        //    // replace everything and keep only IP
        //    return ipAddress.
        //        Replace("<html><head><title>Current IP Check</title></head><body>Current IP Address: ", string.Empty).
        //        Replace("</body></html>", string.Empty);
        //}

        //static private void StreamRead(NetworkStream stream)
        //{
        //    Byte[] bytes = new Byte[256];
        //    string data;
        //    int i;
        //    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
        //    {
        //        // Translate data bytes to a ASCII string.
        //        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
        //        Console.WriteLine("Received: {0}", data);
        //    }
        //    closeSocket = true;
        //}

        //static private void StreamWrite(NetworkStream stream)
        //{
        //    string message = Console.ReadLine();
        //    while (message != "exit")
        //    {
        //        Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

        //        // Send the message to the connected TcpServer. 
        //        stream.Write(data, 0, data.Length);

        //        //Console.WriteLine("Sent: {0}", message);
        //        message = Console.ReadLine();
        //    }
        //    closeSocket = true;
        //}

        //static private void HandleConnection(ref TcpClient client)
        //{
        //    NetworkStream stream = client.GetStream();

        //    Thread readThread = new Thread(() => StreamRead(stream));
        //    Thread writeThread = new Thread(() => StreamWrite(stream));
        //    readThread.Start();
        //    writeThread.Start();
        //    while (!closeSocket) { }

        //    if (readThread.IsAlive) { readThread.Abort(); }
        //    if (writeThread.IsAlive) { writeThread.Abort(); }
        //    stream.Close();
        //    client.Close();
        //}

        static void Main(string[] args)
        {
            //closeSocket = false;
            //TcpListener server = null;
            //try
            //{
            //    Int32 port = 13000;
            //    IPAddress localAddr = Dns.GetHostAddresses(Dns.GetHostName())
            //        .First(a => a.AddressFamily == AddressFamily.InterNetwork);
            //    string ip = GetComputer_InternetIP();
            //    Console.WriteLine("Local ip: " + localAddr);
            //    Console.WriteLine("Server IP: " + ip);
            //    // TcpListener server = new TcpListener(port);
            //    server = new TcpListener(localAddr, port);

            //    // Start listening for client requests.
            //    server.Start();

            //    while (true)
            //    {
            //        Console.Write("Waiting for a connection... ");
            //        TcpClient client = server.AcceptTcpClient();
            //        Console.WriteLine("Connected!");
            //        Thread clientThread = new Thread(() => HandleConnection(ref client));
            //        clientThread.Start();
            //    }
            //}
            //catch (SocketException e)
            //{
            //    Console.WriteLine("SocketException: {0}", e);
            //}
            //finally
            //{
            //    // Stop listening for new clients.
            //    server.Stop();
            //}
            //HostThread host = new HostThread();
            GameState.IsHost = true;
            HostThread.Listen();

            Console.WriteLine("\nHit enter to continue...");
            Console.Read();
        }
    }
}
